package cn.legend.travel.attachment.controller;

import cn.legend.travel.attachment.controller.abs.FileUpload;
import cn.legend.travel.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/upload")
@Api(tags = "文件上传模块")
public class AttachmentController extends FileUpload {

    @PostMapping("image/user-avatar")
    @ApiOperation("上传用户头像")
    public JsonResult uploadUserAvatar(@RequestParam("file") MultipartFile multipartFile,
                                       @ApiIgnore @Value("${travel.upload.user-avatar.max-size}") Integer userAvatarMaxSize,
                                       @ApiIgnore @Value("${travel.upload.user-avatar.types}") List<String> userAvatarTypes) throws Throwable {
        return JsonResult.ok(upload(multipartFile, userAvatarMaxSize, userAvatarTypes, DIR_NAME_USER_AVATAR));
    }

    @PostMapping("image/guide")
    @PreAuthorize("hasAuthority('/articles/issue')")
    @ApiOperation("上传攻略图片")
    public JsonResult uploadGuideImage(@RequestParam("file") MultipartFile multipartFile,
                                       @ApiIgnore @Value("${travel.upload.guide-image.max-size}") Integer guideImageMaxSize,
                                       @ApiIgnore @Value("${travel.upload.guide-image.types}") List<String> guideImageTypes) throws Throwable {
        return JsonResult.ok(upload(multipartFile, guideImageMaxSize, guideImageTypes, DIR_NAME_GUIDE_IMAGE));
    }

    @PostMapping("image/review")
    @PreAuthorize("hasAuthority('/reviews/addNew')")
    @ApiOperation("上传点评图片")
    public JsonResult uploadReviewImage(@RequestParam("file") MultipartFile multipartFile,
                                       @ApiIgnore @Value("${travel.upload.review-image.max-size}") Integer reviewImageMaxSize,
                                       @ApiIgnore @Value("${travel.upload.review-image.types}") List<String> reviewImageTypes) throws Throwable {
        return JsonResult.ok(upload(multipartFile, reviewImageMaxSize, reviewImageTypes, DIR_NAME_REVIEW_IMAGE));
    }

    @PostMapping("image/goods")
    @PreAuthorize("hasAuthority('/goods/addNew')")
    @ApiOperation("上传商品图片")
    public JsonResult uploadGoodsImage(@RequestParam("file") MultipartFile multipartFile,
                                       @ApiIgnore @Value("${travel.upload.goods-image.max-size}") Integer goodsImageMaxSize,
                                       @ApiIgnore @Value("${travel.upload.goods-image.types}") List<String> goodsImageTypes) throws Throwable {
        return JsonResult.ok(upload(multipartFile, goodsImageMaxSize, goodsImageTypes, DIR_NAME_GOODS_IMAGE));
    }

    @PostMapping("image/hotel")
    @PreAuthorize("hasAuthority('/hotel/addNew')")
    @ApiOperation("上传酒店图片")
    public JsonResult uploadHotelImage(@RequestParam("file") MultipartFile multipartFile,
                                       @ApiIgnore @Value("${travel.upload.hotel-image.max-size}") Integer hotelImageMaxSize,
                                       @ApiIgnore @Value("${travel.upload.hotel-image.types}") List<String> hotelImageTypes) throws Throwable {
        return JsonResult.ok(upload(multipartFile, hotelImageMaxSize, hotelImageTypes, DIR_NAME_HOTEL_IMAGE));
    }

    @PostMapping("image/city")
    @PreAuthorize("hasAuthority('/districts/addNew')")
    @ApiOperation("上传城市图片")
    public JsonResult uploadCityImage(@RequestParam("file") MultipartFile multipartFile,
                                       @ApiIgnore @Value("${travel.upload.city-image.max-size}") Integer cityImageMaxSize,
                                       @ApiIgnore @Value("${travel.upload.city-image.types}") List<String> cityImageTypes) throws Throwable {
        return JsonResult.ok(upload(multipartFile, cityImageMaxSize, cityImageTypes, DIR_NAME_CITY_IMAGE));
    }

    @PostMapping("image/scenery")
    @PreAuthorize("hasAuthority('/sceneries/addNew')")
    @ApiOperation("上传景点图片")
    public JsonResult uploadSceneryImage(@RequestParam("file") MultipartFile multipartFile,
                                       @ApiIgnore @Value("${travel.upload.scenery-image.max-size}") Integer sceneryImageMaxSize,
                                       @ApiIgnore @Value("${travel.upload.scenery-image.types}") List<String> sceneryImageTypes) throws Throwable {
        return JsonResult.ok(upload(multipartFile, sceneryImageMaxSize, sceneryImageTypes, DIR_NAME_SCENERY_IMAGE));
    }

    @PostMapping("image/delicacy")
    @PreAuthorize("hasAuthority('/delicacies/addNew')")
    @ApiOperation("上传美食图片")
    public JsonResult uploadDelicacyImage(@RequestParam("file") MultipartFile multipartFile,
                                         @ApiIgnore @Value("${travel.upload.delicacy-image.max-size}") Integer delicacyImageMaxSize,
                                         @ApiIgnore @Value("${travel.upload.delicacy-image.types}") List<String> delicacyImageTypes) throws Throwable {
        return JsonResult.ok(upload(multipartFile, delicacyImageMaxSize, delicacyImageTypes, DIR_NAME_DELICACY_IMAGE));
    }

    @PostMapping("image/banner")
    @PreAuthorize("hasAuthority('/banners/update')")
    @ApiOperation("上传轮播图")
    public JsonResult uploadBanner(@RequestParam("file") MultipartFile multipartFile,
                                   @ApiIgnore @Value("${travel.upload.banner-image.max-size}") Integer bannerImageMaxSize,
                                   @ApiIgnore @Value("${travel.upload.banner-image.types}") List<String> bannerImageTypes) throws Throwable {
        return JsonResult.ok(upload(multipartFile, bannerImageMaxSize, bannerImageTypes, DIR_NAME_BANNER_IMAGE));
    }
}
